package learn.java.basic.util.zip;

import learn.java.basic.util.HexUtil;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.DeflaterInputStream;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;

public class UserInflaterStream {

    public static void main(String[] args) throws IOException {
        String inputStr="aaaaaaa好啊bbbbbbbbb";
        byte[] inputBytes = inputStr.getBytes();
        System.out.println("字节数："+inputBytes.length);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(inputBytes);

        DeflaterInputStream deflaterInputStream = new DeflaterInputStream(byteArrayInputStream);
        byte[] compressBytes = new byte[inputBytes.length];
       int leng =  deflaterInputStream.read(compressBytes);
        System.out.println("压缩后：字节数-"+leng);
        deflaterInputStream.close();

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(outputStream);
        deflaterOutputStream.write(inputBytes,0,inputBytes.length);
        deflaterOutputStream.flush();
        deflaterOutputStream.close();
        compressBytes = outputStream.toByteArray();
        System.out.println("压缩后：字节数-"+compressBytes.length);

        InflaterInputStream inflaterInputStream = new InflaterInputStream(new ByteArrayInputStream(compressBytes));
        outputStream.reset();
        byte[] bytes = new byte[8];
        while (inflaterInputStream.read(bytes)>0){
         outputStream.write(bytes);
        }
        System.out.println("解压缩后的字节："+ new String(outputStream.toByteArray()));

    }

}
